package com.mytrip.webapp.action.login;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.mytrip.base.action.StrutsActionAdapter;
import com.mytrip.model.City;
import com.mytrip.model.News;
import com.mytrip.model.Trip;
import com.mytrip.service.NewsManager;
import com.mytrip.service.TripManager;
import com.mytrip.util.LoginUtil;
import com.mytrip.webapp.filter.CtxSessionBag;

public class FrontHomeAction extends StrutsActionAdapter {

	private static final long serialVersionUID = -6936597562169593729L;

	private TripManager tripManager;

	private NewsManager newsManager;

	private List<Trip> tripList;

	private List<News> newsList;

	private List<City> tripCity;

	private int tripCityCnt;

	private String currentYearStr;

	private Map<String, String> statisticsYear;

	private Map<String, String> statisticsTotal;

	// ~ method
	// =================================================================
	public String home() throws Exception {

		// 最新行程(next)
		tripList = tripManager.queryNextTrip(0, 2);
		for (Trip trip : tripList) {
			trip.setFromCityStr(tripManager.queryCityById(trip.getFromCity())
					.getName());
			trip.setToCityStr(tripManager.queryCityById(trip.getToCity())
					.getName());
			trip.setNotifyUserList(tripManager.queryNotifyUserByTrip(trip
					.getId()));
		}

		// 旅行城市
		tripCity = tripManager.queryTripCitys();
		tripCityCnt = tripCity.size();

		// 新闻
		newsList = newsManager.queryNews(0, 6);

		// 统计
		int tripCntYear = 0;
		int tripCntTotal = 0;
		long tripDateCntYear = 0;
		long tripDateCntTotal = 0;
		List<Integer> toCityIdListYear = new LinkedList<Integer>();
		List<Integer> toCityIdListTotal = new LinkedList<Integer>();

		Calendar currentYear = Calendar.getInstance();
		currentYear.set(Calendar.MONTH, 0);
		currentYear.set(Calendar.DATE, 1);
		currentYear.set(Calendar.HOUR_OF_DAY, 0);
		currentYear.set(Calendar.MINUTE, 0);
		currentYear.set(Calendar.SECOND, 0);
		Date currentYearDate = currentYear.getTime();
		currentYearStr = currentYear.get(Calendar.YEAR) + "";

		List<Trip> allFinishedTrip = tripManager.queryAllFinishedTrip();
		for (Trip trip : allFinishedTrip) {
			Date startDate = trip.getStartDate();
			Date endDate = trip.getEndDate();
			long a = (endDate.getTime() - startDate.getTime())
					/ (1000 * 60 * 60 * 24) + 1;
			Integer toCityId = trip.getToCity();

			if (endDate.after(currentYearDate)) {
				tripCntYear++;
				tripDateCntYear += a;
				if (!toCityIdListYear.contains(toCityId)) {
					toCityIdListYear.add(toCityId);
				}
			}

			tripCntTotal++;
			tripDateCntTotal += a;
			if (!toCityIdListTotal.contains(toCityId)) {
				toCityIdListTotal.add(toCityId);
			}
		}

		statisticsYear = new HashMap<String, String>();
		statisticsTotal = new HashMap<String, String>();

		statisticsYear.put("tripCntYear", tripCntYear + "");
		statisticsYear.put("tripDateCntYear", tripDateCntYear + "");
		statisticsYear.put("tripCityCntYear", toCityIdListYear.size() + "");

		statisticsTotal.put("tripCntTotal", tripCntTotal + "");
		statisticsTotal.put("tripDateCntTotal", tripDateCntTotal + "");
		statisticsTotal.put("tripCityCntTotal", toCityIdListTotal.size() + "");

		return SUCCESS;
	}

	public String logout() {
		LoginUtil.clearCookie();
		CtxSessionBag.clear();
		return "logout";
	}

	public void setTripManager(TripManager tripManager) {
		this.tripManager = tripManager;
	}

	public List<Trip> getTripList() {
		return tripList;
	}

	public void setTripList(List<Trip> tripList) {
		this.tripList = tripList;
	}

	public int getTripCityCnt() {
		return tripCityCnt;
	}

	public void setTripCityCnt(int tripCityCnt) {
		this.tripCityCnt = tripCityCnt;
	}

	public Map<String, String> getStatisticsYear() {
		return statisticsYear;
	}

	public void setStatisticsYear(Map<String, String> statisticsYear) {
		this.statisticsYear = statisticsYear;
	}

	public Map<String, String> getStatisticsTotal() {
		return statisticsTotal;
	}

	public void setStatisticsTotal(Map<String, String> statisticsTotal) {
		this.statisticsTotal = statisticsTotal;
	}

	public String getCurrentYearStr() {
		return currentYearStr;
	}

	public void setCurrentYearStr(String currentYearStr) {
		this.currentYearStr = currentYearStr;
	}

	public void setNewsManager(NewsManager newsManager) {
		this.newsManager = newsManager;
	}

	public List<News> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<News> newsList) {
		this.newsList = newsList;
	}

	public List<City> getTripCity() {
		return tripCity;
	}

	public void setTripCity(List<City> tripCity) {
		this.tripCity = tripCity;
	}

}
